/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.business;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.modules.ivm.dao.business.HangStoreDao;
import com.neusoft.lohas.modules.ivm.entity.business.HangStore;

/**
 * 商圈商店Service
 * @author Jackie.Liu
 * @version 2015-01-29
 */
@Component
@Transactional(readOnly = true)
public class HangStoreService extends BaseService {

	@Autowired
	private HangStoreDao hangStoreDao;
	
	public HangStore get(String id) {
		return hangStoreDao.get(id);
	}
	
	public Page<HangStore> find(Page<HangStore> page, HangStore hangStore) {
		DetachedCriteria dc = hangStoreDao.createDetachedCriteria();
		dc.add(Restrictions.eq(HangStore.FIELD_DEL_FLAG, HangStore.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", hangStore.getMerchant().getId()));
		dc.addOrder(Order.asc("sort"));
		return hangStoreDao.find(page, dc);
	}
	
	public List<HangStore> findList(String merchantId){
		DetachedCriteria dc = hangStoreDao.createDetachedCriteria();
		dc.add(Restrictions.eq(HangStore.FIELD_DEL_FLAG, HangStore.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", merchantId));
		dc.addOrder(Order.asc("sort"));
		return hangStoreDao.find(dc);
	}
	
	public List<HangStore> findChoicedList(HangStore hangStore){
		DetachedCriteria dc = hangStoreDao.createDetachedCriteria();
		dc.add(Restrictions.eq(HangStore.FIELD_DEL_FLAG, HangStore.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", hangStore.getMerchant().getId()));
		dc.add(Restrictions.ne("id", StringUtils.isBlank(hangStore.getId())?"":hangStore.getId()));
		dc.addOrder(Order.asc("sort"));
		return hangStoreDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(HangStore hangStore) {
		hangStoreDao.clear();
		hangStoreDao.save(hangStore);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		hangStoreDao.deleteById(id);
	}

}
